<template>
  <el-card>
    <el-form label-width="120px"
             label-position="right"
             :model="formData"
             :rules="rules"
             ref="form"
             size="small"
             style="width: 900px; min-width: 900px">
      <el-form-item label="公司编号"
                    prop="carrierTenantCode"
                    class="characterBox">
        <TenantCodeInput @input="handleInviteTenant" />
      </el-form-item>
      <el-form-item label="公司全称"
                    prop="carrierName"
                    style="margin-bottom: 18px">
        <el-input placeholder="请输入公司全称"
                  disabled
                  :value="formData.carrierName" />
      </el-form-item>
      <el-form-item label="承运方简称"
                    prop="carrierAbbr"
                    style="margin-bottom: 18px">
        <el-input placeholder="请输入承运方简称"
                  v-model.trim="formData.carrierAbbr"
                  maxlength="20" />
      </el-form-item>
      <el-form-item label="委托对账"
                    prop="autoBalance"
                    class="characterBox">
        <auto-balance v-model="formData.autoBalance" />
      </el-form-item>
      <el-form-item label="邀请信"
                    prop="welcome"
                    style="margin-bottom: 18px">
        <el-input placeholder="请输入邀请信"
                  type="textarea"
                  v-model.trim="formData.welcome"
                  maxlength="100" />
      </el-form-item>
      <el-form-item label=" ">
        <el-button @click="$router.go(-1)">返回</el-button>
        <el-button @click="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
import TenantCodeInput from "@basic/TenantCodeInput.vue";
import AutoBalance from "@/components/advance/AutoBalance";
import { ValidateRules } from "@/libs/util";
import { tenant } from "@/api/index";
export default {
  name: "AddCarrier",
  components: { TenantCodeInput, AutoBalance },
  data () {
    return {
      rules: {
        carrierTenantCode: [ValidateRules.required],
        carrierAbbr: [ValidateRules.required],
      },
      formData: {
        welcome: "",
        carrierTenantCode: "",
        carrierName: "",
        carrierAbbr: "",
        autoBalance: null
      }
    };
  },
  methods: {
    handleInviteTenant (tenant) {
      this.formData.carrierName = tenant.name;
      this.formData.carrierTenantCode = tenant.code;
      this.$refs.form.validate();
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.insert();
        }
      });
    },
    async insert () {
      let { code, message } = await tenant.insert(this.formData);
      this.$message[code === 0 ? "success" : "error"](message);
      if (code === 0) {
        this.$router.go(-1);
      }
    }
  }
};
</script>
